//
//  WHScollIngNoticeView.m
//  WHEasyBizLink
//
//  Created by ShengMeng on 9/25/25.
//



#import "WHScollIngNoticeView.h"

@interface WHScollIngNoticeView ()
@property (nonatomic, strong) UILabel *currentLabel;
@property (nonatomic, strong) UILabel *nextLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) CGFloat offsetY;
@property (nonatomic, assign) BOOL isAnimating;
@end

@implementation WHScollIngNoticeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.clipsToBounds = YES;
    
    // 标题标签（第一行）
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 20)];
    _titleLabel.text = @"公告";
    _titleLabel.textColor = [UIColor redColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:_titleLabel];
    
    // 更多按钮（第一行）
    _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _moreButton.frame = CGRectMake(self.bounds.size.width - 60, 0, 50, 20);
    [_moreButton setTitle:@"更多" forState:UIControlStateNormal];
    [_moreButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    _moreButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [_moreButton addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_moreButton];
    
    // 公告内容（第二行）
    CGFloat contentWidth = self.bounds.size.width - 40;
    CGFloat contentY = 30; // 第二行起始Y坐标
    _currentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, contentY, contentWidth, 20)];
    _nextLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, contentY + 20, contentWidth, 20)];
    
    _currentLabel.textColor = _textColor ?: [UIColor blackColor];
    _nextLabel.textColor = _textColor ?: [UIColor blackColor];
    _currentLabel.font = _font ?: [UIFont systemFontOfSize:14];
    _nextLabel.font = _font ?: [UIFont systemFontOfSize:14];
    
    [self addSubview:_currentLabel];
    [self addSubview:_nextLabel];
}

// 其他方法实现保持不变...
- (void)setNotices:(NSArray<NSString *> *)notices {
    _notices = [notices copy];
    _currentIndex = 0;
    [self resetLabels];
}

- (void)resetLabels {
    if (_notices.count == 0) return;
    
    _currentLabel.text = _notices[_currentIndex];
    _nextLabel.text = _notices[(_currentIndex + 1) % _notices.count];
    
    CGFloat contentY = 30;
    _currentLabel.frame = CGRectMake(20, contentY, self.bounds.size.width - 40, 20);
    _nextLabel.frame = CGRectMake(20, contentY + 20, self.bounds.size.width - 40, 20);
}

- (void)startScrolling {
    if (_displayLink || _notices.count <= 1) return;
    
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateScrollPosition)];
    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)stopScrolling {
    [_displayLink invalidate];
    _displayLink = nil;
}

- (void)updateScrollPosition {
    if (_isAnimating) return;
    
    _offsetY += 0.5;
    CGFloat contentY = 40;
    
    if (_offsetY >= 20) {
        _isAnimating = YES;
        [UIView animateWithDuration:0.3 animations:^{
            self.currentLabel.frame = CGRectMake(20, contentY - 20, self.bounds.size.width - 40, 20);
            self.nextLabel.frame = CGRectMake(20, contentY, self.bounds.size.width - 40, 20);
        } completion:^(BOOL finished) {
            self.currentIndex = (self.currentIndex + 1) % self.notices.count;
            [self resetLabels];
            self.offsetY = 0;
            self.isAnimating = NO;
        }];
    } else {
        _currentLabel.frame = CGRectMake(20, contentY - _offsetY, self.bounds.size.width - 40, 20);
        _nextLabel.frame = CGRectMake(20, contentY + 20 - _offsetY, self.bounds.size.width - 40, 20);
    }
}

-(void)setTitleText:(NSString *)titleText

{
    _titleText = titleText;
    _titleLabel.text = titleText;
    
}

-(void)setMoreButtonText:(NSString *)moreButtonText
{
    _moreButtonText = moreButtonText;
    [_moreButton setTitle:moreButtonText forState:UIControlStateNormal];
}

- (void)moreButtonClicked {
    NSLog(@"更多按钮被点击");
}

- (void)dealloc {
    [self stopScrolling];
}

@end
